# coding:utf-8

import time
import random
from selenium import webdriver
from common.config import config_option

source = u'人民网'
classify = u'著名评论员文章'


class PeopleDailySearch(object):
    def __init__(self):
        self.newContent_list = []
        self.base_url = "http://www.baidu.com/"
        self.web_src = 'PeopleDailySearch'
        self.commentator_list = [u"国平", u"仲祖文", u"钟轩理", u"任仲平", u"何振华"]

    def run_people_daily_search(self):
        """运行人民网主站搜索"""
        for commentator in self.commentator_list[:1]:
            browser = webdriver.PhantomJS(
                r'D:\workspace\python\ak-spider\\resources\\libs\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
            browser.set_window_size(2560, 1600)

            # 首页搜索
            browser.get(self.base_url)
            browser.save_screenshot("首页.png")
            browser.find_element_by_id("kw").send_keys(commentator)
            browser.find_element_by_id("su").click()
            time.sleep(10)

            # 搜索内容列表
            main_handle = browser.current_window_handle  # 获取当前窗口句柄
            essay_info = browser.find_element_by_id('1')
            print("文章信息", essay_info)
            item_title = essay_info.find_element_by_tag_name('a').text
            print("该页主题", item_title)
            essay_info.find_element_by_tag_name('a').click()
            time.sleep(random.randint(5, 10))

            # 具体搜索内容
            handles = browser.window_handles  # 获取所有句柄
            print("句柄数", handles)

            # 切换句柄
            browser.switch_to.window(handles[1])
            print("其他标签", browser.title)

            # 关闭新窗口，切回主页
            browser.close()
            browser.switch_to.window(main_handle)
            print("主页主题", browser.title)

            browser.quit()


if __name__ == "__main__":
    search = PeopleDailySearch()
    search.run_people_daily_search()
